//
//  UIViewController+Additional.m
//  RaiyiCommon
//
//  Created by zhiwei ma on 12-4-25.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "UIViewController+Additional.h"
#import "UIBarButtonItem+Additional.h"

@implementation UIViewController (Additional)

- (void)setNavigationBarButtonItemWithImage:(UIImage*)aIcon itemType:(RYCNavigationbarButtonItemType)aType bgImage:(UIImage*)aBg
{
    UIBarButtonItem* item = nil;
    if (nil == aBg)
    {
        if (RYCNavigationbarButtonItem_Left == aType)
        {
            item = [[UIBarButtonItem alloc] initWithImage:aIcon style:UIBarButtonItemStyleBordered target:self action:@selector(navigationLeftButtonItemClicked:)];
            self.navigationItem.leftBarButtonItem = item;
        }
        else if (RYCNavigationbarButtonItem_Right == aType)
        {
            item = [[UIBarButtonItem alloc] initWithImage:aIcon style:UIBarButtonItemStyleBordered target:self action:@selector(navigationRightButtonItemClicked:)];
            self.navigationItem.rightBarButtonItem = item;
        }
    }
    else
    {
        if (RYCNavigationbarButtonItem_Left == aType)
        {
            item = [[UIBarButtonItem alloc] initWithCustom:aIcon
                                                   bgImage:aBg
                                                    target:self
                                                    action:@selector(navigationLeftButtonItemClicked:)];
            self.navigationItem.leftBarButtonItem = item;
            
        }
        else if (RYCNavigationbarButtonItem_Right == aType)
        {
            item = [[UIBarButtonItem alloc] initWithCustom:aIcon
                                                   bgImage:aBg
                                                    target:self
                                                    action:@selector(navigationRightButtonItemClicked:)];
            self.navigationItem.rightBarButtonItem = item;
        }
    }

    [item release];
}

- (void)setNavigationBarButtonItemWithTitle:(NSString*)aTitle bgImage:(UIImage*)aImage itemType:(RYCNavigationbarButtonItemType)aType
{
    [self setNavigationBarButtonItemWithTitle:aTitle bgImage:aImage itemType:aType useTitleWidth:NO];
}

- (void)setNavigationBarButtonItemWithTitle:(NSString*)aTitle bgImage:(UIImage*)aImage itemType:(RYCNavigationbarButtonItemType)aType useTitleWidth:(BOOL) useTitleWidth
{
    UIBarButtonItem* item = nil;
    
    if (RYCNavigationbarButtonItem_Left == aType)
    {
        item = [[UIBarButtonItem alloc] initWithCustomWithTitle:aTitle
                                                        bgImage:aImage
                                                         target:self
                                                         action:@selector(navigationLeftButtonItemClicked:)
                                                useTitleWidth:useTitleWidth];
        self.navigationItem.leftBarButtonItem = item;
        
    }
    else if (RYCNavigationbarButtonItem_Right == aType)
    {
        item = [[UIBarButtonItem alloc] initWithCustomWithTitle:aTitle
                                                        bgImage:aImage
                                                         target:self
                                                         action:@selector(navigationRightButtonItemClicked:)
                                                useTitleWidth:useTitleWidth];
        self.navigationItem.rightBarButtonItem = item;
    }
    
    [item release];
}

- (void)setNavigationBarButtonItemWithTitle:(NSString*)aTitle bgImage:(UIImage*)aImage itemType:(RYCNavigationbarButtonItemType)aType useImageWidth:(BOOL) useImageWidth
{
    UIBarButtonItem* item = nil;
    if (RYCNavigationbarButtonItem_Left == aType)
    {
        item = [[UIBarButtonItem alloc] initWithCustomWithTitle:aTitle
                                                        bgImage:aImage
                                                         target:self
                                                         action:@selector(navigationLeftButtonItemClicked:)
                                                            useBgImageWidth:useImageWidth];
        self.navigationItem.leftBarButtonItem = item;
        
    }
    else if (RYCNavigationbarButtonItem_Right == aType)
    {
        item = [[UIBarButtonItem alloc] initWithCustomWithTitle:aTitle
                                                        bgImage:aImage
                                                         target:self
                                                         action:@selector(navigationRightButtonItemClicked:)
                                                            useBgImageWidth:useImageWidth];
        self.navigationItem.rightBarButtonItem = item;
    }
    
    [item release];
}

- (void)setNavigationBarButtonItemWithTitleStytlePlain:(NSString*)aTitle itemType:(RYCNavigationbarButtonItemType)aType
{
    [self setNavigationBarButtonItemWithTitle:aTitle bgImage:nil itemType:RYCNavigationbarButtonItem_Right useTitleWidth:YES];
}

- (void)setNavigationBarButtonItemWithSystemItem:(UIBarButtonSystemItem)systemItem itemType:(RYCNavigationbarButtonItemType)aType
{
    UIBarButtonItem* item = nil;
    if (RYCNavigationbarButtonItem_Left == aType)
    {
        item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:systemItem target:self action:@selector(navigationLeftButtonItemClicked:)];
        self.navigationItem.leftBarButtonItem = item;
        
    }
    else if (RYCNavigationbarButtonItem_Right == aType)
    {
        item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:systemItem target:self action:@selector(navigationRightButtonItemClicked:)];
        self.navigationItem.rightBarButtonItem = item;
    }
    [item release];
}

- (void)setNavigationBarButtonClickableState:(BOOL)isClickable itemType:(RYCNavigationbarButtonItemType)aType
{
    if (RYCNavigationbarButtonItem_Left == aType)
    {
        self.navigationController.visibleViewController.navigationItem.leftBarButtonItem.enabled = isClickable;
    }
    else if (RYCNavigationbarButtonItem_Right == aType)
    {
        self.navigationController.visibleViewController.navigationItem.rightBarButtonItem.enabled = isClickable;
    }
}

- (void)navigationLeftButtonItemClicked:(id)aSender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)navigationRightButtonItemClicked:(id)aSender
{
    //noop
}


@end


@implementation UIViewController(ewnav)
- (void)popToBelowViewController
{
    NSArray* array = self.navigationController.viewControllers;
    UIViewController* below = nil;
    for (UIViewController* vc in array)
    {
        if (vc == self)
        {
            break;
        }
        below = vc;
    }
    
    if (below)
    {
        [self.navigationController popToViewController:below animated:YES];
    }
}

@end
